package com.lc.baseres.dagger.module;

import android.app.Application;

import com.lc.baselib.net.intercept.GlobalHttpHandler;
import com.lc.baselib.net.intercept.NetworkIntercept;
import com.lc.baselib.net.manager.IRepositoryManager;
import com.lc.baselib.net.manager.Impl.RepositoryManagerImpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Lazy;
import dagger.Module;
import dagger.Provides;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author LiuChao
 * @describe
 * @date 2018/6/25
 * @contact email:450127106@qq.com
 */
@Module
public class ClientModule {
    private static final int TIMEOUT_WRITE = 60 * 1000;
    private static final int TIMEOUT_READ = 60 * 1000;
    private static final int TIMEOUT_CONNECT = 10 * 1000;

    private String baseUrl;
    private GlobalHttpHandler mGlobalHttpHandler;

    public ClientModule(String baseUrl, GlobalHttpHandler globalHttpHandler) {
        this.baseUrl = baseUrl;
        mGlobalHttpHandler = globalHttpHandler;
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofitClient(OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .build();
        return retrofit;
    }

    @Provides
    @Singleton
    public OkHttpClient provideHttpClient(Interceptor networkIntercept, List<Interceptor> interceptorList) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .addNetworkInterceptor(networkIntercept)
                .writeTimeout(TIMEOUT_WRITE, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT_READ, TimeUnit.SECONDS)
                .connectTimeout(TIMEOUT_CONNECT, TimeUnit.SECONDS);
        if (interceptorList != null && !interceptorList.isEmpty()) {
            for (int i = 0; i < interceptorList.size(); i++) {
                builder.addInterceptor(interceptorList.get(i));
            }
        }
        return builder.build();
    }

    @Provides
    @Singleton
    public IRepositoryManager provideRepositoryManager(Retrofit retrofit, Application application) {
        return new RepositoryManagerImpl(retrofit, application);
    }

    @Provides
    @Singleton
    public Interceptor provideNetworkIntercept() {
        return new NetworkIntercept(mGlobalHttpHandler);
    }

    @Provides
    @Singleton
    public List<Interceptor> provideIntercepts() {
        List<Interceptor> interceptorList = new ArrayList<>();
        // 添加一个全局http拦截请求，每次请求前进行拦截
        if (mGlobalHttpHandler != null) {
            interceptorList.add(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    return chain.proceed(mGlobalHttpHandler.onHttpRequestBefore(chain, chain.request()));
                }
            });
        }
        return interceptorList;
    }


}
